package com.zhuifeng.orders.service.impl;

import java.util.List;
import java.util.concurrent.*;

import com.zhuifeng.common.core.utils.DateUtils;
import com.zhuifeng.common.core.utils.snow.GuuidUtil;
import com.zhuifeng.orders.mapper.OrdersDetailsMapper;
import com.zhuifeng.system.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhuifeng.orders.domain.OrdersDetails;
import com.zhuifeng.orders.service.IOrdersDetailsService;

/**
 * 订单详情Service业务层处理
 * 
 * @author zhuifeng
 * @date 2022-08-18
 */
@Service
public class OrdersDetailsServiceImpl implements IOrdersDetailsService
{
    @Autowired
    private OrdersDetailsMapper ordersDetailsMapper;
    @Autowired
    private SiteServiceImpl siteService;

    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,
            200,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(10000),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());


    /**
     * 查询订单详情
     * 
     * @param ordersDetailsId 订单详情主键
     * @return 订单详情
     */
    @Override
    public OrdersDetails selectOrdersDetailsByOrdersDetailsId(String ordersDetailsId)
    {
        return ordersDetailsMapper.selectOrdersDetailsByOrdersDetailsId(ordersDetailsId);
    }

    /**
     * 查询订单详情列表
     * 
     * @param ordersDetails 订单详情
     * @return 订单详情
     */
    @Override
    public List<OrdersDetails> selectOrdersDetailsList(OrdersDetails ordersDetails)
    {
        return ordersDetailsMapper.selectOrdersDetailsList(ordersDetails);
    }

    /**
     * 新增订单详情
     * 
     * @param ordersDetails 订单详情
     * @return 结果
     */
    @Override
    public int insertOrdersDetails(OrdersDetails ordersDetails)
    {
        ordersDetails.setCreateTime(DateUtils.getNowDate());
        ordersDetails.setOrdersDetailsId(GuuidUtil.getUUID()+"");
        return ordersDetailsMapper.insertOrdersDetails(ordersDetails);
    }

    /**
     * 修改订单详情
     * 
     * @param ordersDetails 订单详情
     * @return 结果
     */
    @Override
    public int updateOrdersDetails(OrdersDetails ordersDetails)
    {
        LoginUser loginUser = new LoginUser();
        ordersDetails.setUpdateTime(DateUtils.getNowDate());
        return ordersDetailsMapper.updateOrdersDetails(ordersDetails);
    }

    /**
     * 批量删除订单详情
     * 
     * @param ordersDetailsIds 需要删除的订单详情主键
     * @return 结果
     */
    @Override
    public int deleteOrdersDetailsByOrdersDetailsIds(String[] ordersDetailsIds)
    {
        return ordersDetailsMapper.deleteOrdersDetailsByOrdersDetailsIds(ordersDetailsIds);
    }

    /**
     * 删除订单详情信息
     * 
     * @param ordersDetailsId 订单详情主键
     * @return 结果
     */
    @Override
    public int deleteOrdersDetailsByOrdersDetailsId(String ordersDetailsId)
    {
        return ordersDetailsMapper.deleteOrdersDetailsByOrdersDetailsId(ordersDetailsId);
    }

    /**
     * 查看订单详情
     * @param ordersId 订单id
     * @return
     */
    @Override
    public OrdersDetails ordersDetailsId(String ordersId) throws ExecutionException, InterruptedException {
        CompletableFuture<OrdersDetails> checkStockCompletableFuture = CompletableFuture.supplyAsync(() -> {
            OrdersDetails ordersDetails = this.ordersDetailsMapper.ordersDetailsId(ordersId);
            return ordersDetails;
        }, threadPoolExecutor);
        return checkStockCompletableFuture.get();
    }
}
